package com.soul.soulcommon.detail.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.soul.soulcommon.detail.DoctorDetail;
import com.soul.soulcommon.dto.Jwt;
import com.soul.souldao.domain.Doctor;
import com.soul.souldao.mapper.DoctorMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Service("doctorDetailService")
@Slf4j
public class DoctorDetailService implements UserDetailsService {
    @Autowired
    private DoctorMapper doctorMapper;
    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        log.info("医生进行登录===========================");
        Doctor doctor = doctorMapper.selectOne(new LambdaQueryWrapper<Doctor>().eq(Doctor::getUserName, userName));
        if(doctor == null){
            throw new UsernameNotFoundException("用户或密码不正确");
        }
        Jwt jwt = new Jwt(doctor.getId(), doctor.getUserName(), doctor.getPassword(), doctor.getRoleName());
        DoctorDetail doctorDetail = new DoctorDetail(jwt);
        return doctorDetail;
    }
}
